# pip install zmail
import random
import time
import zmail
import smtplib
from email.mime.text import MIMEText
import os
def send_email(Customer):
    # 第三方 SMTP 服务
    mail_host = "smtp.qq.com"  # SMTP服务器
    mail_user = "1299246255@qq.com"  # 用户名
    mail_pass = "mbktmpypcuctbacd"  # 密码(这里的密码不是登录邮箱密码，而是授权码)

    sender = '1299246255@qq.com'  # 发件人邮箱
    receivers = [Customer]  # 接收人邮箱
    num_str = ''.join(str(random.randint(0, 9)) for _ in range(5))
    print(num_str)
    content = f'{num_str}'
    title = '验证码(有效期为5分钟) !'  # 邮件主题
    message = MIMEText(content, 'plain', 'utf-8')  # 内容, 格式, 编码
    message['From'] = "{}".format(sender)
    message['To'] = ",".join(receivers)
    message['Subject'] = title
    try:
      smtpObj = smtplib.SMTP_SSL(mail_host, 465)  # 启用SSL发信, 端口一般是465
      smtpObj.login(mail_user, mail_pass)  # 登录验证
      smtpObj.sendmail(sender, receivers, message.as_string())  # 发送
      print("mail has been send successfully.")
      return num_str, time.time()
    except smtplib.SMTPException as e:
      print(e)


if __name__ == '__main__':
  send_email('1299246255@qq.com')
